<?php

class Module_Account_Controller_User extends Engine_Controller
{
    public function indexAction(){

    }

    public function logoutAction() {
        $request = Application_Bootstrap::getRequest();
        unset($_SESSION["auth"]);
        $request->redirect('/');
    }

    public function loginAction() {
        $activeUser = Application_Bootstrap::getActiveUser();
        $request = Application_Bootstrap::getRequest();
        $this->getView()->setLayout('Empty');
        $params = $this->getRequest()->getParams();
        $form = new Module_Account_Form_Login('signin', array('name' => 'Вход в систему'));
        $form->populate($params);
        if ($_POST && $form->isValid()) {
            $authorize = $activeUser->checkAuthorize($params['login'], $params['password']);
            if ($authorize) {
                $tRole = new Module_Account_Table_Role();
                $authData['remoteAddr'] = $_SERVER['REMOTE_ADDR'];
                $authData['time'] = time();
                $authData['firstname'] = $authorize['firstname'];
                $authData['lastname'] = $authorize['lastname'];
                $authData['role'] = $tRole->getRoleName($authorize['role']);
                $authData['id'] = $authorize['id'];
                $_SESSION['auth'] = $authData;
                $request->redirect('/');
            } else {
                $form->addError('Неверные данные');
            }
        }
        $this->getView()->form = $form;

    }
}